package com.xinyue.mqsystem.mq;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import com.xinyue.mqsystem.exception.GameMQSendFailedException;

public class GameMQTemplate {

	private RocketMQTemplate mqTemplate;
	private static Logger logger = LoggerFactory.getLogger(GameMQTemplate.class);
	
	public GameMQTemplate(RocketMQTemplate rocketMQTemplate) {
		this.mqTemplate = rocketMQTemplate;
	}
	
	public void syncSendOrderly(String topic,byte[] data,Object selector) throws GameMQSendFailedException {
		Message<byte[]> message = MessageBuilder.withPayload(data).build();
        SendResult sendResult = mqTemplate.syncSendOrderly(topic, message, selector.toString());
        if(sendResult.getSendStatus() != SendStatus.SEND_OK) {
        	logger.error("RocketMQ发送消息失败，{}",sendResult);
        	throw new GameMQSendFailedException(sendResult.toString());
        }
	}
	
	
}
